package com.wwwarehouse.contract.orders.place_order;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.MotionEvent;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.custom_widget.keyboard.VirtualKeyboardView;
import com.wwwarehouse.common.custom_widget.popupwindow.EasyPopupWindow;
import com.wwwarehouse.common.eventbus_event.BackListenerEvent;
import com.wwwarehouse.common.tools.PopUpUtils;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.contract.bean.ConfirmOrderParams;
import com.wwwarehouse.contract.bean.OrderDetailListBean;
import com.wwwarehouse.contract.bean.OrderDetailParams;
import com.wwwarehouse.contract.bean.ShopingCarTotalPriceBean;
import com.wwwarehouse.contract.core.BaseViewPagerFragment;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.event.RefreshGouWuCarEvent;
import com.wwwarehouse.contract.event.RefreshShoppCardEvent;
import com.wwwarehouse.contract.event.ShoppingCarQtyEvent;

import java.util.HashMap;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * Created by feng.gao on 2017/10/18.
 * 商品明细 viewpager
 */

public class OrderDetailViewPagerFragment extends BaseViewPagerFragment implements View.OnClickListener, EasyPopupWindow.ChildClickListener {
	private String mTotalPrice;
	private String mName;
	private String mLogo;
	private String supplierBusinessId;
	private String demandId;
	private OrderDetailParams orderDetailParams;
	@Override
	public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		EventBus.getDefault().register(this);
		mConfirmBtn.setEnabled(true);
		mConfirmBtn.setBackground(mActivity.getResources().getDrawable(R.drawable.blue_btn_circle));
		mConfirmBtn.setOnClickListener(this);
		mCustomScroll.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				return true;
			}
		});
	}

	@Override
	protected void onResponseFail(String response, int what) {
		super.onResponseFail(response, what);
		if (what == 3) {
			toast(response);
		} else if (what == 2) {
			toast("请重新确认商品");
		}
	}

	@Override
	public void onResponse(String response, int what) {
		if (what == 1) {
			OrderDetailListBean orderDetailListBean = JSON.parseObject(response, OrderDetailListBean.class);
			if (orderDetailListBean != null && orderDetailListBean.getList().size() > 0) {
				if (StringUtils.isNoneNullString(mTotalPrice)) {
					orderDetailListBean.setTotalPrice(mTotalPrice);
				}
				if (StringUtils.isNoneNullString(mLogo)) {
					orderDetailListBean.setLogo(mLogo);
				}
				if (StringUtils.isNoneNullString(mName)) {
					orderDetailListBean.setName(mName);
				}
				if (StringUtils.isNoneNullString(supplierBusinessId)) {
					orderDetailListBean.setSupplierBusinessId(supplierBusinessId);
				}
				if (StringUtils.isNoneNullString(demandId)) {
					orderDetailListBean.setDemanId(demandId);
				}
				mConfirmBtn.setVisibility(View.VISIBLE);
				if (Integer.valueOf(orderDetailListBean.getTotal())> 999) {
					mConfirmBtn.setText("999+\n去下单");
				} else {
					mConfirmBtn.setText(orderDetailListBean.getTotal() + "\n去下单");
				}
				setData(orderDetailListBean, Integer.valueOf(orderDetailListBean.getTotal()), 4, OrderDetailFragment.class.getName());

			} else {
				mRlBkg.setVisibility(View.INVISIBLE);
				mStateLayout.showEmptyView(true);
				mConfirmBtn.setVisibility(View.INVISIBLE);
				mStateLayout.setEmptyListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						requestDatas();
					}
				});
			}
		} else if (what == 2) {
			Bundle bundle = new Bundle();
			ConfirmOrderParams params = new ConfirmOrderParams();
			params.setDemandId(demandId);
			params.setSupplierBusinessId(supplierBusinessId);
			params.setLogo(mLogo);
			params.setName(mName);
			bundle.putSerializable(ContractConstant.KEY_BUNDLE_CONFIRM_ORDER, params);
			ConfirmOrderFragment confirmOrderFragment = new ConfirmOrderFragment();
			confirmOrderFragment.setArguments(bundle);
			pushFragment(confirmOrderFragment);
		} else if (what == 3) {
			ShopingCarTotalPriceBean bean = JSON.parseObject(response, ShopingCarTotalPriceBean.class);
			if (bean != null) {
				if (orderDetailParams != null) {
					orderDetailParams.setTotalPrice(String.valueOf(bean.getTotalPrice()));
				}
				if (getArguments() != null && getArguments().getSerializable(ContractConstant.KEY_BUNDLE_ORDER_DETAILS_PARAMS) != null) {
					orderDetailParams = (OrderDetailParams) getArguments().getSerializable(ContractConstant.KEY_BUNDLE_ORDER_DETAILS_PARAMS);
					if (orderDetailParams != null) {
						Map<String, Object> map = new HashMap<>();
						if (StringUtils.isNoneNullString(orderDetailParams.getTotalPrice())) {
							mTotalPrice = orderDetailParams.getTotalPrice();
						}
						if (StringUtils.isNoneNullString(orderDetailParams.getLogo())) {
							mLogo = orderDetailParams.getLogo();
						}
						if (StringUtils.isNoneNullString(orderDetailParams.getSupplierName())) {
							mName = orderDetailParams.getSupplierName();
						}
						if (StringUtils.isNoneNullString(orderDetailParams.getDemandId())) {
							demandId = orderDetailParams.getDemandId();
							map.put("demandId", orderDetailParams.getDemandId());
						}
						if (StringUtils.isNoneNullString(orderDetailParams.getSupplierBusinessId())) {
							supplierBusinessId = orderDetailParams.getSupplierBusinessId();
							map.put("supplierBusinessId", orderDetailParams.getSupplierBusinessId());
						}
						map.put("page", 1);
						map.put("size", 4);

						sendProgressRequest(ContractConstant.SHOPPING_CART_DETAIL_LIST, map, 1);
					}
				}
			}

		}

	}

	@Override
	public void setTitle() {
		if (peekFragment() instanceof OrderDetailViewPagerFragment) {
			mActivity.setTitle(R.string.order_detail_title);
		}
	}

	@Override
	public void requestDatas() {
		mRlBkg.setVisibility(View.INVISIBLE);
		mConfirmBtn.setVisibility(View.INVISIBLE);
		if (getArguments() != null && getArguments().getSerializable(ContractConstant.KEY_BUNDLE_ORDER_DETAILS_PARAMS) != null) {
			orderDetailParams = (OrderDetailParams) getArguments().getSerializable(ContractConstant.KEY_BUNDLE_ORDER_DETAILS_PARAMS);
			if (orderDetailParams != null) {
				Map<String, Object> map = new HashMap<>();
				if (StringUtils.isNoneNullString(orderDetailParams.getTotalPrice())) {
					mTotalPrice = orderDetailParams.getTotalPrice();
				}
				if (StringUtils.isNoneNullString(orderDetailParams.getLogo())) {
					mLogo = orderDetailParams.getLogo();
				}
				if (StringUtils.isNoneNullString(orderDetailParams.getSupplierName())) {
					mName = orderDetailParams.getSupplierName();
				}
				if (StringUtils.isNoneNullString(orderDetailParams.getDemandId())) {
					demandId = orderDetailParams.getDemandId();
					map.put("demandId", orderDetailParams.getDemandId());
				}
				if (StringUtils.isNoneNullString(orderDetailParams.getSupplierBusinessId())) {
					supplierBusinessId = orderDetailParams.getSupplierBusinessId();
					map.put("supplierBusinessId", orderDetailParams.getSupplierBusinessId());
				}
				map.put("page", 1);
				map.put("size", 4);

				sendRequest(ContractConstant.SHOPPING_CART_DETAIL_LIST, map, 1);
			}
		}

	}

	@Override
	public void onClick(View v) {
		if (v == mConfirmBtn) {
			//这里要先调用下确认订单接口，成功之后再跳转

			//先请求接口是否成功，然后跳转到确认订单页面
			Map<String, Object> map = new HashMap<>();
			map.put("demandId", demandId);
			map.put("supplierBusinessId", supplierBusinessId);
			sendProgressRequest(ContractConstant.CONFIRM_ORDER_METHOD, map, 2);


		}
	}

	public void onEventMainThread(RefreshShoppCardEvent event) {
		if (peekFragment() instanceof OrderDetailViewPagerFragment) {
			if (StringUtils.isNoneNullString(event.getRefresh())) {
				if (orderDetailParams != null) {
					orderDetailParams.setTotalPrice(String.valueOf(event.getTotalPrice()));
				}
				requestDatas();
			} else {
				requestDatas();
			}
		}


	}

	//	public void onEventMainThread(OrderDetailOnItemClickEvent event) {
//		if (peekFragment() instanceof OrderDetailViewPagerFragment) {
//			if (mKeyBoard.getVisibility() == View.VISIBLE) {
//				mKeyBoard.setVisibility(View.GONE);
//			}
//		}
//	}
	EasyPopupWindow mEasyPopupWindow;
	ShoppingCarQtyEvent mShopEvent;
	public void onEventMainThread(final ShoppingCarQtyEvent event) {
		if (peekFragment() instanceof OrderDetailViewPagerFragment) {
			if (event != null) {
				mEasyPopupWindow = PopUpUtils.showPopUpBottom(R.layout.contract_f_key_board_pop_layout, mActivity, mActivity.findViewById(android.R.id.content), true, this);
				mShopEvent = event;
			}
		}


	}

	public void onEventMainThread(BackListenerEvent backListenerEvent) {
		if (peekFragment() instanceof OrderDetailViewPagerFragment && backListenerEvent.getMsg().equals("OrderDetailViewPagerFragment")) {
			popFragment();
			//刷新购物车
			EventBus.getDefault().post(new RefreshGouWuCarEvent("refresh" ));

		}
	}


	@Override
	public void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}


	@Override
	public void getChildView(int i, View view) {
		VirtualKeyboardView keyboardView = (VirtualKeyboardView) view.findViewById(R.id.number);
		keyboardView.isShowContentBar(true);
		keyboardView.setOnInputDataConfirmListener(new VirtualKeyboardView.OnInputDataConfirmListener() {
			@Override
			public void InputDataConfirm(String s) {
				if(StringUtils.isNullString(s)){
					mEasyPopupWindow.dismiss();
					return;
				}

				try {
					if (StringUtils.isNoneNullString(s) && mShopEvent != null) {
						if (Long.valueOf(s) == 0) {
							toast("数量不能为0");
							mEasyPopupWindow.dismiss();
							return;
						}
						if (Long.valueOf(s) > Long.valueOf(mShopEvent.getKuCunCount())) {
							toast("当前商品库存不足");
						} else {
							//去请求接口
							Map<String, Object> map = new HashMap<>();
							map.put("cartItemUkid", mShopEvent.getCartItemUkid());
							map.put("demandId", mShopEvent.getDemandId());
							map.put("fuction", mShopEvent.getFuction());
							map.put("rsQty", s);
							map.put("supplierBusinessId", mShopEvent.getSupplierBusinessId());
							sendProgressRequest(ContractConstant.UPDATE_CART_ITEM_METHOD, map, 3);
						}
						mEasyPopupWindow.dismiss();

					}

				} catch (NumberFormatException e) {
					toast("请输入有效数量！");
					mEasyPopupWindow.dismiss();

				}
			}
		});
	}
}
